package com.xxg.renrenorder.controller;

import com.xxg.renrencommon.domain.result.BaseResult;
import com.xxg.renrencommon.domain.result.PageInfoResult;
import com.xxg.renrenorder.dao.bean.OrderMasterInfo;
import com.xxg.renrenorder.domain.query.*;
import com.xxg.renrenorder.domain.result.OrderMasterInfoResult;
import com.xxg.renrenorder.service.OrderService;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 订单管理接口
 *
 * @author szj
 * @date 2021/11/29
 */
@Api(tags = "订单管理")
@RestController
@RequestMapping("/order")
@AllArgsConstructor
public class OrderController {
    private final OrderService orderService;

    @ApiOperation("分页获取各种状态的订单（不含售后订单）")
    @RequestMapping(value = "getOrdersByStatus", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "orderListQuery", value = "分页条件", required = true, dataType = "AppOrderListQuery")})
    public BaseResult<List<OrderMasterInfo>> getOrdersByStatus(@RequestBody @Validated AppOrderListQuery orderListQuery, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return orderService.getOrdersByStatus(userId, orderListQuery.getOrderStatus(),
                orderListQuery.getPageNo(), orderListQuery.getPageSize());
    }

    @ApiOperation("后台接口分页获取所有订单")
    @RequestMapping(value = "getAllOrdersByStatus", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "orderListQuery", value = "分页条件", required = true, dataType = "OrderListQuery")})
    public BaseResult<PageInfoResult<OrderMasterInfo>> getAllOrdersByStatus(@RequestBody @Validated OrderListQuery orderListQuery, HttpServletRequest request) {
        String shopId = request.getHeader("shopId");
        String account = request.getHeader("account");
        return orderService.getAllOrdersByStatus(orderListQuery.getOrderStatus(), orderListQuery.getPageNo(),
                orderListQuery.getPageSize(), orderListQuery.getStartDate(), orderListQuery.getEndDate(), orderListQuery, shopId, account);
    }

    @ApiOperation("结算")
    @RequestMapping(value = "settleAccounts", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "query", value = "结算信息", required = true, dataType = "SettleAccountsQuery")})
    public BaseResult<OrderMasterInfo> settleAccounts(@RequestBody @Validated SettleAccountsQuery query, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return orderService.settleAccounts(userId, query);
    }

    @ApiOperation("提交订单")
    @RequestMapping(value = "submitOrder", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "query", value = "提交订单参数", required = true, dataType = "SubmitOrderQuery")})
    public BaseResult<String> submitOrder(@RequestBody @Validated SubmitOrderQuery query, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return orderService.submitOrder(userId, query);
    }

    @ApiOperation("后台接口设置普通订单的物流信息")
    @RequestMapping(value = "setOneOrderExpress", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "query", value = "物流信息", required = true, dataType = "SetOneOrderExpressQuery")})
    public BaseResult<Void> setOneOrderExpress(@RequestBody @Validated SetOneOrderExpressQuery query, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        String shopId = request.getHeader("shopId");
        String account = request.getHeader("account");
        return orderService.setOneOrderExpress(query, userId, shopId, account);
    }

    @ApiOperation("取消普通订单")
    @RequestMapping(value = "cancelOrderById", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "orderQuery", value = "6订单ID", required = true, dataType = "OrderQuery")})
    public BaseResult<Void> cancelOrderById(@RequestBody @Validated OrderQuery orderQuery, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return orderService.cancelOrderById(orderQuery.getId(), userId);
    }

    @ApiOperation("普通订单确认收货")
    @RequestMapping(value = "commitOrderById", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "orderQuery", value = "5订单ID", required = true, dataType = "OrderQuery")})
    public BaseResult<Void> commitOrderById(@RequestBody @Validated OrderQuery orderQuery, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        String shopId = request.getHeader("shopId");
        String account = request.getHeader("account");
        return orderService.commitOrderById(orderQuery.getId(), userId, shopId, account);
    }

    @ApiOperation("更新带货商品底下的所有库存和销量")
    @RequestMapping(value = "updateCarryProduct", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "query", value = "更新带货商品底下的所有库存和销量", required = true, dataType = "UpdateCarryProductQuery")})
    public BaseResult<Void> updateCarryProduct(@RequestBody @Validated UpdateCarryProductQuery query) {
        return orderService.updateCarryProduct(query);
    }

    @ApiOperation("查看普通订单详情")
    @RequestMapping(value = "getOrderById", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "orderQuery", value = "1订单ID", required = true, dataType = "OrderQuery")})
    public BaseResult<OrderMasterInfoResult> getOrderById(@RequestBody @Validated OrderQuery orderQuery, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return orderService.getOrderById(orderQuery.getId(), userId);
    }

    @ApiOperation("删除普通订单")
    @RequestMapping(value = "removeOrderById", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "orderQuery", value = "3订单ID", required = true, dataType = "OrderQuery")})
    public BaseResult<Void> removeOrderById(@RequestBody @Validated OrderQuery orderQuery, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return orderService.removeOrderById(orderQuery.getId(), userId);
    }
}
